package com.mq.controller;

import com.mq.pojo.RespondMessage;
import com.mq.pojo.User;
import com.mq.producer.UserProducer;
import com.mq.service.FlightService;
import com.mq.service.OrderService;
import com.mq.service.TravelRecommendationsService;
import com.mq.service.UserService;
import com.mq.service.impl.WechatPay;
import com.mq.utils.JwtUtil;
import dev.langchain4j.agent.tool.P;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.mq.utils.HashUtils.getMd5String;

/**
 * 用户控制器
 * 使用消息队列处理所有用户相关请求
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserProducer userProducer;
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public RespondMessage registerUser(
            @Pattern(regexp = "^\\S{5,16}$") String username,
            String Email,
            @Pattern(regexp = "^\\S{5,16}$") String password,
            String phone
    ) {
        userProducer.sendRegisterMessage(Email, username, password, phone);
        return RespondMessage.success("注册请求已提交");
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public RespondMessage login(String Email,
                              @Pattern(regexp = "^\\S{5,16}$") String password) {
        userProducer.sendLoginMessage(Email, password);
        
        // 获取用户信息生成token
        User loginUser = userService.getUserByUserEmail(Email);
        if (loginUser != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("email", Email);
            claims.put("userId", loginUser.getUserId());
            String token = JwtUtil.genToken(claims);
            
            // 存储token到Redis
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(token, token, 12, TimeUnit.HOURS);
            
            return RespondMessage.success(token);
        }
        return RespondMessage.error("登录失败");
    }

    /**
     * 获取旅行推荐列表
     */
    @GetMapping("/getTravelRecommendationsList")
    public RespondMessage getTravelRecommendationsList(@RequestHeader(name = "Authorization") String token,
                                                     Integer page, Integer count) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        String userEmail = (String) map.get("email");
        userProducer.sendGetTravelRecommendationsMessage(userEmail, page, count);
        return RespondMessage.success("获取旅行推荐列表请求已提交");
    }

    /**
     * 获取航班列表
     */
    @GetMapping("/getFlightsList")
    public RespondMessage getFlightsList(String Destination) {
        userProducer.sendGetFlightsListMessage(Destination);
        return RespondMessage.success("获取航班列表请求已提交");
    }

    /**
     * 锁定订单
     */
    @PostMapping("/lockOrder")
    public RespondMessage lockOrder(@RequestHeader(name = "Authorization") String token,
                                  Integer flightId) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        String userEmail = (String) map.get("email");
        userProducer.sendLockOrderMessage(userEmail, flightId);
        return RespondMessage.success("锁定订单请求已提交");
    }

    /**
     * 取消订单
     */
    @PostMapping("/cancelOrder")
    public RespondMessage cancelOrder(@RequestHeader(name = "Authorization") String token,
                                    Integer flightId) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        String userEmail = (String) map.get("email");
        userProducer.sendCancelOrderMessage(userEmail, flightId);
        return RespondMessage.success("取消订单请求已提交");
    }

    /**
     * 支付订单
     */
    @PostMapping("/payOrder")
    public RespondMessage payOrder(@RequestHeader(name = "Authorization") String token,
                                 Integer orderId) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        String userEmail = (String) map.get("email");
        userProducer.sendPayOrderMessage(userEmail, orderId);
        return RespondMessage.success("支付订单请求已提交");
    }

    /**
     * 获取订单列表
     */
    @GetMapping("/getOrderList")
    public RespondMessage getOrderList(@RequestHeader(name = "Authorization") String token) {
        Map<String, Object> map = JwtUtil.parseToken(token);
        String userEmail = (String) map.get("email");
        userProducer.sendGetOrderListMessage(userEmail);
        return RespondMessage.success("获取订单列表请求已提交");
    }
}


